"""DHF 水文模型参数范围定义"""


class ParameterRanges:
    """DHF 水文模型参数范围（基于 para_schema.json）"""
    
    # 初始状态参数范围
    INITIAL_STATE_RANGES = {
        "SA0": {"min": 0, "max": 50, "unit": "mm", "name": "初始表层蓄水量"},
        "UA0": {"min": 0, "max": 90, "unit": "mm", "name": "初始下层蓄水量"},
        "YA0": {"min": 0, "max": 100, "unit": "mm", "name": "初始前期影响雨量"}
    }
    
    # DHF 模型参数范围
    MODEL_PARAMETER_RANGES = {
        "A": {"min": 0, "max": 5, "unit": "", "name": "蓄水空间分布参数"},
        "B": {"min": 1, "max": 3, "unit": "", "name": "下渗率形状系数"},
        "F": {"min": 0, "max": 10000, "unit": "km²", "name": "流域面积"},
        "G": {"min": 0, "max": 1, "unit": "", "name": "不透水面积比例"},
        "K": {"min": 0.1, "max": 0.9, "unit": "", "name": "表层出流系数"},
        "L": {"min": 0, "max": 500, "unit": "km", "name": "流域最大河长"},
        "N": {"min": 2, "max": 6, "unit": "", "name": "地下与地表汇流时间比"},
        "B0": {"min": 0.5, "max": 2, "unit": "", "name": "特征河长系数"},
        "CC": {"min": 0.5, "max": 4, "unit": "", "name": "地表汇流峰度参数"},
        "D0": {"min": 70, "max": 160, "unit": "mm", "name": "地下水蓄水容量"},
        "DD": {"min": 0.5, "max": 4, "unit": "", "name": "地表汇流形状参数"},
        "K0": {"min": 0, "max": 0.8, "unit": "", "name": "汇流底宽指数"},
        "K2": {"min": 0.2, "max": 0.8, "unit": "", "name": "下层下渗系数"},
        "KA": {"min": 0.7, "max": 1, "unit": "", "name": "前期净雨衰减系数"},
        "KW": {"min": 0, "max": 1, "unit": "", "name": "壤中流分配比例"},
        "S0": {"min": 0, "max": 50, "unit": "mm", "name": "表层蓄水容量"},
        "U0": {"min": 0, "max": 90, "unit": "mm", "name": "下层蓄水容量"},
        "CCL": {"min": 0.5, "max": 4, "unit": "", "name": "地下汇流峰度参数"},
        "COE": {"min": 0, "max": 0.8, "unit": "", "name": "峰现时间参数"},
        "DDL": {"min": 0.5, "max": 4, "unit": "", "name": "地下汇流形状参数"},
    }
    
    @classmethod
    def normalize_param_name(cls, param_name: str) -> str:
        """
        标准化参数名（转换为正确的大小写形式）
        
        Args:
            param_name: 用户输入的参数名（可能是小写或混合大小写）
            
        Returns:
            标准化的参数名（大写），如果不存在则返回原值
        """
        # 转换为大写
        upper_name = param_name.upper()
        
        # 检查是否在初始状态参数中
        if upper_name in cls.INITIAL_STATE_RANGES:
            return upper_name
        
        # 检查是否在模型参数中
        if upper_name in cls.MODEL_PARAMETER_RANGES:
            return upper_name
        
        # 如果找不到，返回原值（保持原样，后续会报错）
        return param_name
    
    @classmethod
    def get_range(cls, param_name: str):
        """
        获取参数范围（支持大小写不敏感）
        
        Args:
            param_name: 参数名（支持小写）
            
        Returns:
            参数范围字典，如果不存在返回 None
        """
        # 标准化参数名
        normalized_name = cls.normalize_param_name(param_name)
        
        # 查找范围
        if normalized_name in cls.INITIAL_STATE_RANGES:
            return cls.INITIAL_STATE_RANGES[normalized_name]
        return cls.MODEL_PARAMETER_RANGES.get(normalized_name)

